/**
 * @Author: Bobby
 * @Date: 2022-02-15 13:14:17
 * @LastEditTime: 2022-02-22 10:08:07
 * @LastEditors: Bobby
 * @Description:
 * @FilePath: \wms-v2\src\project\bobby\store\diy.js
 * @CIMC-V2
 */
import cloneDeep from 'lodash/cloneDeep'
import { PAGEINFO } from '@ve/common/constant'
import { removeModule, setModels, getModel } from '@ve/utils/utils'
import isEmpty from 'lodash/isEmpty'
/**
 * @description DIY拖拽页面布局
 */
const state = () => ({
  // 设置相关
  settings: {
    mode: 'pc', //模式 pc mobile
    //辅助线
    auxiliaryLine: true,
    //图层
    layerShow: false,
    //页面图层模态框
    layerModal: false,
    //左右辅助面板
    leftPane: false,
    rightPane: false,
    //数据源弹框
    dataSourceModal: false,
    moduleSelectModal: false,
  },
  pageInfo: {
    ...cloneDeep(PAGEINFO),
    hasMainPage: true,
  },
  //layer
  layer: {
    //状态
    status: '',
    item: {},
  },
  selectLayer: {},
  selectLayerId: '',
  showSetIconMoal: false,
  iconModalData: [],
  //modules
  modules: [], //所有模块
  selectModule: {}, //当前选择模块
  selectModuleId: '', //选择模块id
  pageModules: [], //页面上的模块
})
const getters = {
  //module
  getDataSourceModal: (state) => state.settings.dataSourceModal,
  getSelectModule: (state) => state.selectModule,
  getSetIconMoal: (state) => state.showSetIconMoal,
  getIconModalData: (state) => state.iconModalData,
  getSelectModuleId: (state) => state.selectModuleId,
  getModules: (state) => state.selectModule,
  getModuleSelectModal: (state) => state.settings.moduleSelectModal,
  //page
  getPageModules: (state) => state.pageModules,
  getPageInfo: (state) => state.pageInfo,
  //settings
  getSettings: (state) => state.settings,
  //layer
  getLayer: (state) => state.layer,
  getSelectLayer: (state) => state.selectLayer,
  getSelectLayerId: (state) => state.selectLayerId,
  //deleteinfo
  getDeletePages: (state) => state.deletePages,
}
const mutations = {
  //获取所有模块
  setModules(state, modules) {
    state.modules = modules
  },
  setIconMoalSwitch(state, modules) {
    state.showSetIconMoal = modules
  },
  setIconModalData(state, modules) {
    state.iconModalData = modules
  },
  //获取当前选择模块
  setSelectModule(state, module) {
    state.selectModule = module
  },
  //获取当前选择模块id
  setSelectModuleId(state, id) {
    state.selectModuleId = id
  },
  setPageModules(state, modules) {
    state.pageModules = modules
  },
  setPageInfo(state, data) {
    state.pageInfo = data
  },
  removeModule(state, data) {
    let _list = removeModule(state.pageModules, data._id)
    setModels(_list)
    //先更新当前页面
    state.pageModules = _list
    //再更新图层
    let _layer = getModel([state.pageInfo], state.selectLayerId)
    _layer.pageModules = _list
  },
  removeModuleIds(state, ids) {
    ids.forEach((item) => {
      this.commit('diy/removeModule', item)
    })
  },
  //
  setSettingsObject(state, obj) {
    state.settings = {
      ...state.settings,
      ...obj,
    }
  },
  //设置layer数据
  setLayer(state, data) {
    state.layer = data
  },
  setSelectLayerInfo(state, layer) {
    state.selectLayer = layer
    state.selectLayerId = layer._id
  },
  pushSelectLayerList(state, item) {
    state.selectLayer.list.push(item)
  },
  removeLayer(state, id) {
    let _list = removeModule([state.pageInfo], id)
    state.pageInfo = _list[0]
  },
  //设置删除页数据
  setDeletePages(state, data) {
    if (!data || !data.data) {
      return
    }
    state.deletePages = state.deletePages || {}
    state.deletePages[data.layerid] = state.deletePages[data.layerid] || []
    state.deletePages[data.layerid].push(data.data)
  },
  removeDeletePages(state, data) {
    //撤销后删除最后一个
    state.deletePages[data.layerid] && state.deletePages[data.layerid].pop()
  },
  // 添加复制的图层
  addCopyLayer(state, data) {
    let parentLayer = {}
    function selectParentLayer(obj) {
      if (!isEmpty(parentLayer)) return
      if (obj._id === data.parentId) {
        parentLayer = obj
      }
      if (obj.list) {
        obj.list.forEach((item) => {
          selectParentLayer(item)
        })
      }
    }
    selectParentLayer(state.pageInfo)
    parentLayer.list.push(data.layerData)
  },
}
const actions = {
  //获取所有模块
  setModules({ commit }, modules) {
    commit('setModules', modules)
  },
  //获取选中模块
  setSelectModule({ commit }, module) {
    commit('setSelectModule', module)
  },
  //获取选中模块id
  setSelectModuleId({ commit }, id) {
    commit('setSelectModuleId', id)
  },
  //删除模块
  removeModule({ commit }, module) {
    commit('removeModule', module)
  },
  //删除多个模块
  removeModuleIds({ commit }, ids) {
    commit('removeModuleIds', ids)
  },
  //获取页面所有模块
  setPageModules({ commit }, modules) {
    commit('setPageModules', modules)
  },
  //获取页面信息
  setPageInfo({ commit }, data) {
    commit('setPageInfo', data)
  },
  //setSettingsObject
  setSettingsObject({ commit }, obj) {
    commit('setSettingsObject', obj)
  },
  //Layer
  setLayer({ commit }, data) {
    commit('setLayer', data)
  },
  setSelectLayerInfo({ commit }, id) {
    commit('setSelectLayerInfo', id)
  },
  pushSelectLayerList({ commit }, item) {
    commit('pushSelectLayerList', item)
  },
  removeLayer({ commit }, id) {
    commit('removeLayer', id)
  },
  //删除页面操作
  setDeletePages({ commit }, item) {
    commit('setDeletePages', item)
  },
  removeDeletePages({ commit }, item) {
    commit('removeDeletePages', item)
  },
  addCopyLayer({ commit }, data) {
    commit('addCopyLayer', data)
  },
  showSetIconMoalAction({ commit }, data) {
    commit('setIconMoalSwitch', data)
  },
  setIconModalData({ commit }, data) {
    commit('setIconModalData', data)
  },
}
export default { state, getters, mutations, actions }
